#include "Effect.h"
#include "WEUtility.h"
#include "ResourceManager.h"

using namespace std;

namespace WuguiEngine
{
	namespace Graphics
	{
		BaseEffect::BaseEffect(GraphicsDevice* device, string _filename)
		{
			pDevice = device;
			filename = _filename;

			ResourceManager* pResourceMgr = ResourceManager::GetInstance();
			pD3DEffect = (ID3DXEffect*)(pResourceMgr->GetD3DResource(filename, 
				pDevice, ResourceType::RTEffect));
		}

		void BaseEffect::Begin(unsigned int* pPasses)
		{
			pD3DEffect->Begin(pPasses, 0);
		}

		void BaseEffect::BeginPass(int iPass)
		{
			pD3DEffect->BeginPass(iPass);
		}

		void BaseEffect::EndPass()
		{
			pD3DEffect->EndPass();
		}

		void BaseEffect::End()
		{
			pD3DEffect->End();
		}

		void BaseEffect::SetMatrix(D3DXHANDLE name, D3DXMATRIX* mat)
		{
			pD3DEffect->SetMatrix(name, mat);
		}

		void BaseEffect::SetTexture(D3DXHANDLE name, GameTexture* tex)
		{
			pD3DEffect->SetTexture(name, tex->GetD3D9Texture());
		}

		void BaseEffect::SetVector2(D3DXHANDLE name, D3DXVECTOR2* vec2)
		{
			pD3DEffect->SetValue(name, vec2, sizeof(D3DXVECTOR2));
		}

		void BaseEffect::SetVector3(D3DXHANDLE name, D3DXVECTOR3* vec3)
		{
			pD3DEffect->SetValue(name, vec3, sizeof(D3DXVECTOR3));
		}

		void BaseEffect::SetInt(D3DXHANDLE name, int i)
		{
			pD3DEffect->SetInt(name, i);
		}

		void BaseEffect::SetFloat(D3DXHANDLE name, float f)
		{
			pD3DEffect->SetFloat(name, f);
		}

		void BaseEffect::SetBool(D3DXHANDLE name, bool b)
		{
			pD3DEffect->SetBool(name, b);
		}

		bool BaseEffect::SetTechnique(D3DXHANDLE name)
		{
			if (FAILED(pD3DEffect->SetTechnique(name)))
			{
				return false;
			}

			return true;
		}

		void BaseEffect::Dispose()
		{
			ResourceManager::GetInstance()->DisposeD3DResource(filename);
			pD3DEffect = NULL;
		}
	}
}